<template>
  <div>
    <div class="articaldetail">
      <div class="header">
        <div class="left">
          <van-icon name="arrow-left back"
                    @click="$router.back()" />
          <span class="iconfont iconnew new"></span>
        </div>
        <span :class="{active:artDetail.has_follow}"
              @click="followFn">{{artDetail.has_follow?'已关注':'关注'}}</span>
      </div>
      <div class="detail">
        <div class="title">{{artDetail.title}}</div>
        <div class="desc">
          <span>{{artDetail.user&&artDetail.user.nickname}}</span> &nbsp;&nbsp;
          <span>{{new Date(artDetail.create_date).toLocaleDateString()}}</span>
        </div>
        <!-- 
            文章类型
            视频：type==2
            非视频：type==1
         -->
        <div class="content"
             v-if="artDetail.type==1"
             v-html="artDetail.content">
          文章的内容：
        </div>
        <div class="content"
             v-if="artDetail.type==2">
          <!-- 文章的内容： -->
          <!-- 
              poster：设置视频封面
              controls：视频控制器
           -->
          <video width="100%"
                 :poster="artDetail.cover[0].url|joinPath"
                 controls></video>
        </div>
        <div class="opt">
          <span @click="likeFn(artDetail.id)"
                class="like"
                :class="{unlike:artDetail.has_like}">
            <van-icon name="good-job-o" />{{token?artDetail.like_length:'点赞'}}
          </span>
          <span class="chat">
            <van-icon name="chat"
                      class="w" />微信
          </span>
        </div>
      </div>
      <!-- 精彩跟帖 -->
      <div class="keeps">
        <h2>精彩跟帖</h2>
        <div class="item">
          <div class="head">
            <img src=""
                 alt />
            <div>
              <p>火星网友</p>
              <span>2小时前</span>
            </div>
            <span>回复</span>
          </div>
          <div class="text">文章说得很有道理</div>
        </div>
        <div class="more">更多跟帖</div>
      </div>
    </div>
    <CommentFooter :obj="artDetail" />
  </div>
</template>

<script>
import { articleDetail, post_like } from '../api/news'
import { user_follows, user_unfollow } from '../api/user'
import CommentFooter from '../components/CommentFooter.vue'
export default {
  components: { CommentFooter },
  created() {
    const id = localStorage.getItem('artId')
    articleDetail(id).then((res) => {
      this.artDetail = res.data.data
    })
  },
  data() {
    return {
      artDetail: {},
      token: localStorage.getItem('tokenLine'),
    }
  },
  methods: {
    //   是否关注该用户
    followFn() {
      const api = this.artDetail.has_follow ? user_unfollow : user_follows
      api(this.artDetail.user.id).then((res) => {
        if (res.data.message === '用户信息验证失败') {
          this.$toast.success(res.data.message)
          return
        }
        console.log(res);
        this.$toast.success(res.data.message)
        this.artDetail.has_follow = !this.artDetail.has_follow
      })
    },
    // 是否点赞
    likeFn(id) {
      post_like(id).then((res) => {
        if (res.data.message === '点赞成功') {
          this.artDetail.has_like = true
          this.artDetail.like_length++
          this.$toast.success(res.data.message)
        } else {
          this.artDetail.has_like = false
          this.artDetail.like_length--
          this.$toast.success(res.data.message)
        }
      })
    },
  },
}
</script>

<style lang="less" scoped>
.header {
  padding: 0px 10px;
  height: 50px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #ccc;
  > .left {
    height: 100%;
    vertical-align: middle;
    position: relative;
    .van-icon {
      position: absolute;
      top: 15px;
      left: 0;
    }
    > span {
      margin-left: 22px;
      font-size: 50px;
    }
  }
  > span {
    padding: 5px 15px;
    background-color: #f00;
    color: #fff;
    text-align: center;
    border-radius: 15px;
    font-size: 13px;
    &.active {
      color: #000;
      border: 1px solid #000;
      background-color: #fff;
    }
  }
}
.detail {
  padding: 15px;
  .title {
    font-size: 18px;
    font-weight: bold;
    padding: 10px 0;
  }
  .desc {
    line-height: 30px;
    color: #999;
    font-size: 13px;
  }
  /deep/.content {
    text-indent: 2em;
    line-height: 24px;
    font-size: 15px;
    padding-bottom: 30px;
    width: 100%;
    // 设置v-html渲染的样式，需要样式穿透才能设置
    .photo {
      text-indent: 0;
      img {
        width: 100%;
      }
    }
  }
}
.unlike {
  color: red;
}
.opt {
  display: flex;
  justify-content: space-around;
  .like,
  .chat {
    height: 25px;
    padding: 0 15px;
    font-size: 14px;
    line-height: 25px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 15px;
  }
  .w {
    color: rgb(84, 163, 5);
  }
}
.keeps {
  border-top: 5px solid #ddd;
  padding: 0 15px;
  > h2 {
    line-height: 50px;
    text-align: center;
  }
  .item {
    padding: 10px 0;
    border-bottom: 1px solid #ccc;
    .head {
      display: flex;
      justify-content: space-between;
      align-items: center;
      > img {
        width: (50/360) * 100vw;
        height: (50/360) * 100vw;
        display: block;
        border-radius: 50%;
      }
      > div {
        flex: 1;
        display: flex;
        flex-direction: column;
        margin-left: 10px;
        > span {
          font-size: 12px;
          color: #999;
          line-height: 25px;
        }
      }
      > span {
        color: #999;
        font-size: 13px;
      }
    }
    .text {
      font-size: 14px;
      color: #333;
      padding: 20px 0 10px 0;
    }
  }
  .more {
    width: 100px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 15px;
    margin: 20px auto;
    font-size: 13px;
  }
}
</style>